package com.xc.ksxt.controller;

import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.KnowledgePoint;
import com.xc.ksxt.entity.KnowledgePointEntity;
import com.xc.ksxt.entity.LibCategoryEntity;
import com.xc.ksxt.entity.LibQuestionEntity;
import com.xc.ksxt.service.KnowledgePointService;
import com.xc.ksxt.service.LibCategoryService;
import com.xc.ksxt.vo.KnowledgePointVO;
import com.xc.sys.service.SysUserService;
import com.xc.sys.vo.SysUserVO;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/knowledgePoint")
@CrossOrigin
public class KnowledgePointController {
    @Autowired
    private LibCategoryService libCategoryService;
    @Autowired
    private KnowledgePointService knowledgePointService;
    private KnowledgePoint KLP = KnowledgePoint.KNOWLEDGE_POINT.as("KLP");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<KnowledgePointVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<KnowledgePointVO> ltr = new LayuiTableResponse<>();
        Condition cond = KLP.DEL_FLAG.notEqual(-1);
        String name = request.getParameter("name");
        String categoryId = request.getParameter("categoryId");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if (StringUtils.isNotBlank(name)) {
            cond = cond.and(KLP.NAME.like("%"+name+"%"));
        }
        if (StringUtils.isNotBlank(categoryId)) {
            cond = cond.and(KLP.CATEGORY_ID.eq(Long.valueOf(categoryId)));
        }
        Long count = knowledgePointService.count(cond);
        ltr.setCount(count);
        PageInfo<KnowledgePointEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> list = knowledgePointService.select(cond, pageInfo,KLP.NAME.asc(),KLP.NAME.asc(),KLP.CREATE_TIME.desc());
        List<KnowledgePointVO> knowledgePointEntityList = list.into(KnowledgePointVO.class);
        for (KnowledgePointVO knowledgePointEntity : knowledgePointEntityList){
            SysUserVO user = SysUserService.getFrameCaches(knowledgePointEntity.getCreateUserId());
            knowledgePointEntity.setCreateUserName(user==null?"":user.getName());
            if(StringUtils.isNotEmpty(knowledgePointEntity.getCategoryId())){
                LibCategoryEntity ca = libCategoryService.getEntityById(Long.valueOf(knowledgePointEntity.getCategoryId()));
                knowledgePointEntity.setCategoryName(ca==null?"":ca.getName());
            }

        }
        ltr.setData(knowledgePointEntityList);
        return ltr;
    }

    @RequestMapping({"/getById"})
    public KnowledgePointEntity getById(Long id) {
        KnowledgePointEntity entity = knowledgePointService.getEntityById(id);
        return entity;
    }

    @RequestMapping({"/save"})
    public ResponseEntity<KnowledgePointEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
        ResponseEntity<KnowledgePointEntity> res = new ResponseEntity<>();
        KnowledgePointEntity knowledgePointEntity = Helper.map2Bean(new KnowledgePointEntity(), params);
        knowledgePointService.save(knowledgePointEntity);
        return res;
    }
    @RequestMapping({"/updateOrderno"})
    public ResponseEntity<KnowledgePointEntity> updateOrderno(@RequestBody HashMap<String,String> params) {
        Long id = Long.valueOf(params.get("id"));
        Integer orderno = Integer.valueOf(params.get("orderno"));
        ResponseEntity<KnowledgePointEntity> res = new ResponseEntity<>();
        KnowledgePointEntity lq = knowledgePointService.getEntityById(id);
        lq.setOrderno(orderno);
        knowledgePointService.update(lq);
        return res;
    }
    @RequestMapping({"/delete"})
    public LayuiTableResponse<KnowledgePointEntity> delete(@RequestBody Map<String,String> params) {
        String ids = params.get("ids");
        Long[] idarr = Helper.str2longArr(ids);
        LayuiTableResponse<KnowledgePointEntity> ltr = new LayuiTableResponse<>();
        for (Long id : idarr) {
            KnowledgePointEntity m = knowledgePointService.getEntityById(id);
            m.setDelFlag(-1);
            knowledgePointService.update(m);
        }
        return ltr;
    }

}
